package io.renren.modules.sys.user;

import io.renren.common.utils.JacksonUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

/**
 * 获取用户
 */
public class SecurityUser {

    public static Subject getSubject() {
        try {
            return SecurityUtils.getSubject();
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取用户信息
     * @return
     */
    public static UserDetail getUser() {
        Subject subject = getSubject();
        if(subject == null){
            return new UserDetail();
        }

        Object principal = subject.getPrincipal();
        UserDetail user = JacksonUtils.obj2pojo(principal, UserDetail.class);
        if(user == null){
            return new UserDetail();
        }
        return user;

    }
    /**
     * 获取用户ID
     */
    public static Long getUserId() {
        return getUser().getUserId();
    }

}
